<template>
	<view>
		<view class="content">
			<view class="phone">
				<text>手机号</text>
				<input type="number" maxlength="11" placeholder="请输入11位手机号" v-model="phone" />
			</view>
			<view class="phone">
				<view class="left">
					<text>验证码</text>
					<input type="number" maxlength="4" placeholder="请输入验证码" v-model="checkSMS" />
				</view>
				<view @click="getCode" class="right">{{ regNumber ? '获取验证码' : count + ' s' }}</view>
			</view>
			<view class="phone">
				<text>新密码</text>
				<input type="password" maxlength="16" placeholder="请输入新密码" v-model="password" />
			</view>
			<view class="phone">
				<text>重复新密码</text>
				<input type="password" maxlength="16" placeholder="请再输入一次新密码" v-model="newPassword" />
			</view>
			<view class="footer"><view class="result" @click="saveHandle">确定</view></view>
		</view>
	</view>
</template>

<script>
import { sendSMS, checkSMS, updatePwd } from '@/api/sms/sms.js';
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
export default {
	data() {
		return {
			phone: '',
			password: '',
			phoneReg: false,
			timer: null, //倒计时
			regNumber: true, //获取验证码的间隔
			count: '', //倒计时显示的数字
			getSMS: false, //获取短信
			resSendSMS: false, //是否已发验证码
			checkSMS: '', //检查验证码
			newPassword: ''
		};
	},
	watch: {
		phone(val, oldVal) {
			if (val.length === 11) {
				if (!myreg.test(this.phone)) {
					this.phoneReg = false;
					return false;
				} else {
					this.phoneReg = true;
					return true;
				}
			} else {
				this.phoneReg = false;
				return false;
			}
		}
	},
	methods: {
		async getCode() {
			//获取验证码
			if (this.phoneReg == true) {
				const TIME_COUNT = 60;
				let res = await sendSMS(this.phone);
				if (res.msg == '发送成功') {
					this.resSendSMS = true;
					uni.showToast({
						title: '验证码已发送，请注意查收',
						icon: 'none',
						duration: 1500
					});
					if (!this.timer) {
						this.count = TIME_COUNT;
						this.regNumber = false;
						this.timer = setInterval(() => {
							if (this.count > 0 && this.count <= TIME_COUNT) {
								this.count--;
							} else {
								this.regNumber = true;
								clearInterval(this.timer);
								this.timer = null;
								this.getSMS = false;
							}
						}, 1000);
					}
				} else {
					uni.showToast({
						title: '发送失败，请稍候再试',
						icon: 'none',
						duration: 1500
					});
				}
			} else {
				uni.showToast({
					title: '请输入正确的手机号！',
					icon: 'none',
					duration: 1500
				});
			}
		},
		async saveHandle() {
			if (this.password.length > 0 && this.newPassword.length > 0) {
				if (this.password == this.newPassword) {
					let res = await checkSMS(this.phone, this.checkSMS);
					// res.code==1
					if (res.code==1) {
						let data = await updatePwd(this.password);
						if (data.code == 1) {
							uni.showToast({
								title: res.msg
							});
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								});
							}, 1500);
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				} else {
					uni.showToast({
						title: '两次密码输入不一样！',
						icon: 'none'
					});
				}
			} else {
				uni.showToast({
					title: '新密码和重复密码不能为空！',
					icon: 'none'
				});
			}
		}
	}
};
</script>

<style lang="scss">
@import './css/updatePassword.scss';
</style>
